package com.xr.elm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xr.elm.common.CustomException;
import com.xr.elm.dto.DishDto;
import com.xr.elm.entity.Dish;
import com.xr.elm.entity.DishFlavor;
import com.xr.elm.mapper.DishMapper;
import com.xr.elm.service.DishFlavorService;
import com.xr.elm.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存基本信息到菜品中
        this.save(dishDto);
        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=  flavors.stream().peek((item)-> item.setDishId(dishId)).collect(Collectors.toList());

        //保存菜品口味到表中，dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //根据菜品基本信息，从dish表查询
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据---dish flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据-一dish flavor表的nsert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=  flavors.stream().peek((item)-> item.setDishId(dishDto.getId())).collect(Collectors.toList());

        //保存菜品口味到表中，dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public void removeWithFlavor(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);

//        抛出一个业务异常如果可以删除，
        if (count>0){
            throw new CustomException("菜品正在售卖中，不能删除");
        }
//        先删除套餐表中的数据——setmeal
        this.removeByIds(ids);
//          删除关系表中的数据--setmeal_dish
        //delete from setmealdish where setmeal id in (1,2,3)
        LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(queryWrapper1);
    }

    @Override
    public void sellWithFlavor(Integer status, List<Long> ids) {
        LambdaUpdateWrapper<Dish> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Dish::getId,ids);
        lambdaUpdateWrapper.set(Dish::getStatus,status);
        dishMapper.update(null,lambdaUpdateWrapper);
    }

}
